package com.atguigu.flink;

import com.atguigu.bean.WaterSensor;
import com.atguigu.datasource.RandomWatersensor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.util.Collector;

public class Demo05 {
    public static void main(String[] args) {
        Configuration configuration = new Configuration();
        configuration.setInteger("rest.port",10000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(configuration);
        env.setParallelism(2);

        //对process方法进行测试
        DataStreamSource<WaterSensor> dataSource = env.addSource(new RandomWatersensor());

        //这里是没有进行keyby操作
        //那么这个就是根据并行度来进行计算，有几个并行度就有几个执行分支！
        //因为没有对数据做key进行操作！！
        dataSource.process(new ProcessFunction<WaterSensor, Integer>() {
            //定义一个中间值来接收
            int result = 0;

            @Override
            public void processElement(WaterSensor value, Context ctx, Collector<Integer> out) throws Exception {
                //对传输进来的值进行一个简单的聚合操作
                result +=value.getVc();
                out.collect(result);
            }
        }).print();


        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
